<?php


// Inialize session
session_start();

// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['username'])) {
        header('Location: index.php');
}


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
?>


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>
		Administrative Page
	</title>
	<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
	<meta name="keywords" content="" />
	<meta name="description" content="" />
	<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
	<script type="text/javascript" language="javascript" src="jquery.js"></script> 
	<script type='text/javascript' language="javascript" src="DataTables-1.7.3/media/js/jquery.dataTables.min.js"></script>
	<style type="text/css">
	</style> 
	
	<?php
	include 'functions.php';
	?>

</head>

<body>
	<?php
			include 'headinfo.php';
		?>
	<hr />
	<div id="header">
		<?php
			$thispage = "Administrative";
			require 'menu.php';
		?>
		<!-- end #menu -->
	</div>
	<!-- end #header -->
	<!-- end #header-wrapper -->
	<div id="page">
		<div id="content">
		  	<div class="post">
		  		
			  	<h2 class="title"><a href="#">Administrative</a></h2>
							
				<p class="meta"></p>
				<?php
					$thissubpage ="Edit Crop/Category";
					require 'menu_admin.php';
				?>
				
    	
		<div id="msg">
    	<p name="msg"></p>
    	
    	<?php
    	if(isset($_POST['addCategory'])&&!empty($_POST['categorynameAdd'])){
    	
    	
    	$categoryname = $_POST['categorynameAdd'];
    	//$notes = $_POST['notes'];
    	addCategory($categoryname, NULL);
    	//addCrop($cropname, $firstunit, $secondunit, $categoryid);
    	
    	}
    	
    	?>
       	
       	
       <?php
    		if(isset($_POST['deleteCategory'])&&$_POST['categorynameDelete']!=="select"){
    	
    		$categoryname = $_POST['categorynameDelete'];
    		deleteCategory($categoryname);
    	
    	}
    	
    	?>
    	<?php
    	if(isset($_POST['addCrop'])&&!empty($_POST['cropname'])&&(getCategoryId($_POST['cropcategory'])!==0)){    	
    	$cropname = $_POST['cropname'];
    	$categoryname= $_POST['cropcategory'];
	$firstunit = $_POST['cropfirstunit'];
	$secondunit = $_POST['cropsecondunit'];
    	$categoryid = getCategoryId($categoryname);

	$firstunitid = getUnitId($firstunit);

	if ($secondunit == "0"){
	    	addCrop($cropname, $firstunitid, 0, $categoryid);
	}
	else {
		$secondunitid = getUnitId($secondunit);
    		addCrop($cropname, $firstunitid, $secondunitid, $categoryid);
	}


    	
    	}
    	
    	?>
        
        <?php
    	if(isset($_POST['deleteCrop'])&&$_POST['cropname']!=="select"){
    	
    		$cropname = $_POST['cropname'];
    		deleteCrop($cropname);
    	
    	}
    	
    	?>
    	
    	</div>	
    	
    	
    <div id="table">
    <table class="tablecss">
    <tr>
    <td>
    	<h2>Add Crop</h2>
    	<br />
    	<form name="form" method="post" action="adminPage.php">
		<p>
			Crop Name: 
			<input maxlength="50" name="cropname" type="text" />&nbsp; 
			
			Category: 
			<select name="cropcategory">
			<option value="select">Select Category</option>
			<?php 
            	$allCategories = getAllCategories();
            	for($i = 0; $i<count($allCategories); $i++){
            		echo "<option value=\"".$allCategories[$i]['categoryname']."\">".$allCategories[$i]['categoryname']."</option>";
            	}
            ?>
			</select>
			<br>
			<br>
			First Unit:
			<select name="cropfirstunit">
			<?php 
            	$allUnits = getAllUnitsWithoutOrder();
            	for($i = 0; $i<count($allUnits); $i++){
            		echo "<option value=\"".$allUnits[$i]['unitname']."\">".$allUnits[$i]['unitname']."</option>";
            	}
            ?>
			</select> 
			&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 
			Second Unit:
			<select name="cropsecondunit">
			<option value="0"> none </option>
			<?php 
            	$allUnits = getAllUnitsWithoutOrder();
            	for($i = 0; $i<count($allUnits); $i++){
            		echo "<option value=\"".$allUnits[$i]['unitname']."\">".$allUnits[$i]['unitname']."</option>";
            	}
            ?>
			</select> 
			</td>
			<td>
			<br />
			<br />
			<input name="addCrop" type="submit" value="Add" /></p>
		</form>
	 </td>
	 </tr>
     <tr>
	 
     <td>
    	
    	<h2>Add Category</h2>
    	<br />
    	<form name="form" method="post" action="adminPage.php">
       	<p>
       		Category Name:&nbsp; 
       		<input maxlength="50" name="categorynameAdd" type="text" /> 
       		</td>
			<td>
			<br />
			<br />
       		<input name="addCategory" type="submit" value="Add" />
       	</p>
       	<form>  	
    </td>
	</tr>
	
	<tr>
		<td>
		<h2>Delete Crop</h2>
		<br />
		<form name="form" method="post" action="adminPage.php">
		<p>
			Select Crop: 
			<select name="cropname">
			<option value="select">Select Crop</option>
			<?php 
            	$allCrops = getAllCrops();
            	for($i = 0; $i<count($allCrops); $i++){
            		echo "<option value=\"".$allCrops[$i]['cropname']."\">".$allCrops[$i]['cropname']."</option>";
            	}
            ?>	
			</select>
			</td>
			<td>
			<br />
			<br />
			<input name="deleteCrop" type="submit" value="Delete" /></p>
        </form> 
        </td>
        </tr>
        <tr>
        <td>
       	
       	<h2>Delete Category</h2>
       	<br />
       	<form name="form" method="post" action="adminPage.php">
		<p>
			Select Category: <select name="categorynameDelete">
			<option value="select">Select Category</option>
			<?php 
            	$allCategories = getAllCategories();
            	for($i = 0; $i<count($allCategories); $i++){
            		echo "<option value=\"".$allCategories[$i]['categoryname']."\">".$allCategories[$i]['categoryname']."</option>";
            	}
            ?>
			</select>
			</td>
			<td>
			<br />
			<br />
			<input name="deleteCategory" type="submit" value="Delete" />
		</p>
		</form>
	</td>
     </tr>
     </table>
     </div>
     
          				
			</div>
		</div>
	</div>
	
<div style="clear: both;">&nbsp;</div>
	
<div id="footer">
	<p>Copyright (c) 2010 Dilmun Hill Team.</p>
</div>
<!-- end #footer -->
	
</body>

</html>